package com.xtpt.disaster.controller;
import com.xtpt.disaster.bean.DbnodeInfo;
import com.xtpt.disaster.common.PortalResult;
import com.xtpt.disaster.service.DBNodeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/node")
@Api(tags = "数据库节点管理")
public class DBNodeInfoController {
    @Autowired
    DBNodeInfoService dbNodeInfoService;

    @ResponseBody
    @GetMapping("/getbyid")
    @ApiOperation(value = "根据id查询节点信息")
    public PortalResult getDb(@RequestParam("id") Long id) {
        DbnodeInfo nodeinfo = dbNodeInfoService.selectByPrimaryKey(id);
        return PortalResult.ok(nodeinfo);
    }

    @ResponseBody
    @GetMapping("/all")
    @ApiOperation(value = "返回所有节点信息")
    public PortalResult allDBNode() {
        List<DbnodeInfo> dbnodeInfos = dbNodeInfoService.selectAllNode();
        return PortalResult.ok(dbnodeInfos);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "dbId", value = "数据库节点id", type = "Long"),
            @ApiImplicitParam(name = "nodeId", value = "节点id", type = "Long"),
            @ApiImplicitParam(name = "nodeName", value = "节点名称", type = "String"),
            @ApiImplicitParam(name = "nodeIp", value = "节点IP", type = "String"),
            @ApiImplicitParam(name = "nodeAccount", value = "节点用户名称", type = "String"),
            @ApiImplicitParam(name = "nodePassword", value = "节点用户密码", type = "String"),
            @ApiImplicitParam(name = "nodeOsType", value = "节点操作系统类型", type = "Integer"),
            @ApiImplicitParam(name = "nodeDbName", value = "数据库名称", type = "String"),
            @ApiImplicitParam(name = "nodeDbUsername", value = "数据库用户名", type = "String"),
            @ApiImplicitParam(name = "nodeDbPassword", value = "数据库密码", type = "String"),
            @ApiImplicitParam(name = "nodeDbType", value = "数据库类型", type = "Integer"),
            @ApiImplicitParam(name = "nodeDbPort", value = "数据库端口", type = "Integer"),
            @ApiImplicitParam(name = "nodeDbBinPath", value = "数据库bin文件夹的路径", type = "String"),
            @ApiImplicitParam(name = "nodeDbDatabasePath", value = "数据库表数据存储路径", type = "String"),
            @ApiImplicitParam(name = "nodeDbOwner", value = "Linux客户机数据库目录所有者（组:用户）", type = "String"),
            @ApiImplicitParam(name = "nodeDbServiceName", value = "数据库注册的服务名", type = "String"),
            @ApiImplicitParam(name = "nodeDbBackupPath", value = "客户机文件备份路径", type = "String"),
            @ApiImplicitParam(name = "nodeDbIniPath", value = "客户机数据库配置文件路径", type = "String")
    })
    @ResponseBody
    @PostMapping("/save")
    @ApiOperation(value = "新增node节点")
    public PortalResult saveDBNode(@RequestBody DbnodeInfo dbnodeInfo) {
        int count = dbNodeInfoService.insert(dbnodeInfo);
        if(count == 1) {
            System.out.println("添加成功");
            return PortalResult.ok();
        } else {
            return PortalResult.error("添加节点失败");
        }
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除节点")
//    public PortalResult deleteDBNode(@PathVariable("id") Long id) {
    public PortalResult deleteDBNode(@RequestParam("id") Long id) {
        int count = dbNodeInfoService.deleteById(id);
        System.out.println("开始删除");
        System.out.println(count);
        if(count == 1) {
            System.out.println("删除成功");
            return PortalResult.ok(count);
        } else {
            return PortalResult.error("删除节点失败");
        }
    }
    @PostMapping("/batchDelete")
    @ApiOperation(value = "批量删除节点")
    public PortalResult batchDelete(@RequestBody long[] ids) {
        int count = dbNodeInfoService.batchDelete(ids);
        System.out.println("批量删除");
        if(count >= 1) {
            System.out.println("批量删除成功");
            System.out.println(count);
            return PortalResult.ok(count);
        } else {
            return PortalResult.error("批量删除失败");
        }
    }
    @PostMapping("/update")
    @ApiOperation(value = "根据id更新节点信息")
    public PortalResult updateById(@RequestBody DbnodeInfo dbnodeInfo) {
        int count = dbNodeInfoService.updateById(dbnodeInfo);
        if(count == 1) {
            return PortalResult.ok();
        } else {
            return PortalResult.error("更新节点失败");
        }
    }
}
